<?php

/**
 * 文件注释
 *
 * Author: Liam
 * Datetime: 03/01/18 00:00 PM
 */

namespace ApiBundle\Controller;

use BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class PersonalController extends BaseController
{
    /**
     * 个人信息
     *
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function userAction(Request $request) {

        $token = $request->get('token');

        $conn = $this->get('database_connection');
        $user = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $user['token']) {
            return $this->jsonResponse('操作成功', 0, $user);
        } else {return $this->jsonResponse('登录失效，请重新登录', 5);
        }
    }

    /**
     * 个人信息修改
     *
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function userEditAction(Request $request) {

        $token = $request->get('token');

        $conn = $this->get('database_connection');
        $user = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $user['token']) {
            $data['id'] = $request->get('id');
            $data['name'] = $request->get('name');
            $data['pet_name'] = $request->get('pet_name');
            $data['pet_classify_name'] = $request->get('pet_classify_name');
            $data['age'] = $request->get('age');
            $data['updated_at'] = date('y-m-d h:i:s',time());

            $conn->update("user",$data, array("id" => $data['id']));

            return $this->jsonResponse('操作成功', 0);
        } else {
            return $this->jsonResponse('登录失效，请重新登录', 5);
        }
    }
}